package com.weijianhuawen.wjblog.web.markdown;

import com.weijianhuawen.wjblog.web.markdown.renderer.ImageNodeRenderer;
import com.weijianhuawen.wjblog.web.markdown.renderer.LinkNodeRenderer;
import com.weijianhuawen.wjblog.web.utils.MarkdownEscapedUtil;
import fr.brouillard.oss.commonmark.ext.notifications.Notification;
import fr.brouillard.oss.commonmark.ext.notifications.NotificationsExtension;
import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.ext.image.attributes.ImageAttributesExtension;
import org.commonmark.ext.ins.InsExtension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.Arrays;
import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-31 0:09
 */
public class MarkdownHelper {
    /**
     * Markdown 解析器
     */
    private final static Parser PARSER;
    /**
     * HTML 渲染器
     */
    private final static HtmlRenderer HTML_RENDERER;

    /**
     * 初始化
     */
    static {
        // Markdown 拓展
        List<Extension> extensions = Arrays.asList(
                TablesExtension.create(), // 表格拓展
                HeadingAnchorExtension.create(), // 标题锚定项
                ImageAttributesExtension.create(), // 图片宽高
                TaskListItemsExtension.create(), // 任务列表
                AutolinkExtension.create(), // 自动链接
                StrikethroughExtension.create(), //删除线
                InsExtension.create(),
                NotificationsExtension.create()
                        .withDomElementMapper(new NotificationsExtension.DomElementMapper() {
                    @Override
                    public String domElement(Notification notification) {
                        return "el-alert";
                    }
                }) // 通知块
        );

        PARSER = Parser.builder().extensions(extensions).build();
        HTML_RENDERER = HtmlRenderer.builder()
                .extensions(extensions)
//                .attributeProviderFactory(context -> new NoFollowLinkAttributeProvider())
                .nodeRendererFactory(context -> new ImageNodeRenderer(context))
                .nodeRendererFactory(context -> new LinkNodeRenderer(context))
                .build();
    }



    /**
     * 将 Markdown 转换成 HTML
     * @param markdown
     * @return
     */
    public static String convertMarkdown2Html(String markdown) {
        Node document = PARSER.parse(markdown);
        System.out.println(document.getFirstChild().toString());
        return HTML_RENDERER.render(document);
    }

    public static void main(String[] args) {
//        String markdown = "![Picture](https://imzbf.github.io/md-editor-rt/imgs/mark_emoji.gif)";
        String markdown = "superscript^26^";
        String esm = MarkdownEscapedUtil.escaped(markdown);
        System.out.println(esm);
        System.out.println(MarkdownHelper.convertMarkdown2Html(markdown));
        System.out.println(MarkdownHelper.convertMarkdown2Html(esm));


    }
}
